﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Reflection;
using System.IO;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business
{
    public static class PageHelper
    {
        /// <summary>
        /// 获取文件夹中所有网页文件
        /// </summary>
        /// <returns></returns>
        public static IEnumerable GetAspxPages()
        {
            var root = HttpContext.Current.Server.MapPath("~/");
            return RecursiveGetPages(root, "*.aspx");
        }

        /// <summary>
        /// 获取文件夹中所有网页文件和所属模块
        /// </summary>
        public static List<DbPageInfo> GetAspxPagesInfo()
        {
            var root = HttpContext.Current.Server.MapPath("~/");
            IEnumerable pages = RecursiveGetPages(root, "*.aspx");
            List<DbPageInfo> pagesinfo = new List<DbPageInfo>();
            using (var dc = new CCEntities())
            {

                foreach (var page in pages)
                {
                    int level = (int)AccessLevel.Disabled;
                    DbPageInfo info = new DbPageInfo();

                    info.FullName = page.GetType().GetProperty("FullName").GetValue(page, null) as string;
                    info.DefaultBelong = (SystemModule)Enum.Parse(typeof(SystemModule), page.GetType().GetProperty("BelongsTo").GetValue(page, null).ToString(), true);
                    var dbInfos = dc.AspxPages.Where(p => p.FullName == info.FullName && p.AccessLevel > level && !p.Disabled).ToArray();
                    if (dbInfos != null && dbInfos.Count() != 0)
                    {
                        foreach (var dbinfo in dbInfos)
                        {
                            if (dbinfo.BelongsTo == "CCD")
                                info.IsCCD = "checked";
                            else if (dbinfo.BelongsTo == "CCO")
                                info.IsCCO = "checked";
                            else if (dbinfo.BelongsTo == "CID")
                                info.IsCID = "checked";
                            else if (dbinfo.BelongsTo == "CIO")
                                info.IsCIO = "checked";
                            else if (dbinfo.BelongsTo == "ACR")
                                info.IsACR = "checked";
                            else if (dbinfo.BelongsTo == "EZVisit")
                                info.IsEZVisit = "checked";
                            else if (dbinfo.BelongsTo == "Admin")
                                info.IsAdmin = "checked";
                        }
                    }
                    else
                    {
                        info.IsDefault = "table_tr";
                        if (info.DefaultBelong == SystemModule.CCD)
                        {
                            info.IsCCD = "checked";
                            info.IsCCO = "checked";
                        }
                        else if (info.DefaultBelong == SystemModule.CCO)
                        {
                            info.IsCCD = "checked";
                            info.IsCCO = "checked";
                        }
                        else if (info.DefaultBelong == SystemModule.CID)
                        {
                            info.IsCID = "checked";
                            info.IsCIO = "checked";
                        }
                        else if (info.DefaultBelong == SystemModule.CIO)
                        {
                            info.IsCID = "checked";
                            info.IsCIO = "checked";
                        }
                        else if (info.DefaultBelong == SystemModule.ACR)
                            info.IsACR = "checked";
                        else if (info.DefaultBelong == SystemModule.EZVisit)
                            info.IsEZVisit = "checked";
                        else if (info.DefaultBelong == SystemModule.Admin)
                            info.IsAdmin = "checked";

                    }
                    pagesinfo.Add(info);
                }
            }
            return pagesinfo;
        }

        private static IEnumerable RecursiveGetPages(string root, string filter)
        {
            foreach (var file in Directory.GetFiles(root, filter, SearchOption.AllDirectories))
            {
                yield return new { FullName = "~/" + file.Substring(root.Length).Replace('\\', '/'), BelongsTo = TryGetModule(root, file), };
            }
        }

        private static SystemModule TryGetModule(string root, string file)
        {
            file = file.Substring(root.Length);

            if (file.StartsWith("CIS", StringComparison.OrdinalIgnoreCase))
                return SystemModule.CID;
            else if (file.StartsWith(SystemModule.ACR.ToString(), StringComparison.OrdinalIgnoreCase))
                return SystemModule.ACR;
            else if (file.StartsWith(SystemModule.EZVisit.ToString(), StringComparison.OrdinalIgnoreCase))
                return SystemModule.EZVisit;
            else if (file.StartsWith(SystemModule.Admin.ToString(), StringComparison.OrdinalIgnoreCase))
                return SystemModule.Admin;
            else
                return SystemModule.CCD;
        }

        /// <summary>
        /// 获取用户可访问的所有页面
        /// </summary>
        public static List<AspxPage> GetEmployeeByUsername(string username)
        {
            using (var dc = new CCEntities())
            {
                var pages = dc.SP_GetPagesByEmployeeName(username).ToList<AspxPage>();
                var roles = dc.SP_GetRolesByEmployeeName(username).ToList<Role>();
                foreach (var role in roles)
                {
                    var addPages = dc.AspxPages.Where(p => p.BelongsTo == role.BelongsTo && p.Disabled == false).ToList<AspxPage>();
                    pages.AddRange(addPages);
                }
                return pages;
            }

        }

        /// <summary>
        /// get pages by roles
        /// </summary>
        /// <param name="roleNames"></param>
        /// <returns></returns>
        public static IEnumerable<AspxPage> GetPagesByRoles(IEnumerable<string> roleNames)
        {
            List<AspxPage> pages = new List<AspxPage>();
            if (roleNames == null) return pages;
            using (var dc = new CCEntities())
            {
                foreach (var r in roleNames)
                {
                    var role = dc.Roles.FirstOrDefault(p => p.Name == r && !p.Disabled);
                    if (role == null) continue;
                    pages.AddRange(role.AspxPages.ToArray());
                }
                return pages;
            }
        }


        /// <summary>
        /// 定义页面排序模式
        /// </summary>
        public static readonly string[] SortDirections = new string[] { "asc", "desc" };
    }

    public enum AccessLevel
    {
        Disabled = 0,
        Read = 1,
        Write = 2,
    }

    public class DbPageInfo
    {
        public string FullName { get; set; }
        public string IsCCD { get; set; }
        public string IsCCO { get; set; }
        public string IsCID { get; set; }
        public string IsCIO { get; set; }
        public string IsACR { get; set; }
        public string IsEZVisit { get; set; }
        public string IsAdmin { get; set; }
        public string IsDefault { get; set; }
        public SystemModule DefaultBelong { get; set; }
    }

    public class PageInfo
    {
        public string FullName { get; set; }
        public string BelongsTo { get; set; }
    }
}